//
//  LANBDepositController.m
//  LanBKit_Example
//
//  Created by v on 2020/10/10.
//  Copyright © 2020 v. All rights reserved.
//

#import "LANBDepositController.h"
#import <LanBDepositer.h>
#import <Toast/Toast.h>

@interface                                 LANBDepositController () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray     *aryData;
@end

@implementation LANBDepositController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = UIColor.blackColor;
    // 网络设置
    [LanBNetworking configRequestType:kLanB_RequestTypeJSON responseType:kLanB_ResponseTypeData shouldAutoEncodeUrl:NO callbackOnCancelRequest:YES];
    [LanBNetworking cacheGetRequest:NO shoulCachePost:NO];

    /*
     配置 scheme: wx 小程序、ali sande H5、ali fly H5 无法配置scheme回跳
     1. ali scheme 仅对 原生App 及原生H5有效,
     2. wx h5 scheme 仅对 wx 原生H5有效 且需跟微信后台配置一致!!!，wx 原生App ，直接在info.plist 配置 即可
     */
    [LanBDepositer.shared lanb_setAliScheme:@"comLanBAli" wxH5Scheme:@"pay.vchat.club"];
    
    // 配置测试接口 入参
    self.aryData = @[
        @{
            @"title" : @"微信原生app支付",
            @"version" : @"1.0.1",
            @"platform" : @"Wechat",
        },
        @{
            @"title" : @"微信原生h5支付",
            @"version" : @"1.0.2",
            @"platform" : @"Wechat",
        },
        @{
            @"title" : @"微信杉德小程序支付",
            @"version" : @"1.0.3",
            @"platform" : @"Wechat",
        },
        @{
            @"title" : @"支付宝原生app支付",
            @"version" : @"1.0.1",
            @"platform" : @"Alipay",
        },
        @{
            @"title" : @"支付宝原生h5支付",
            @"version" : @"1.0.2",
            @"platform" : @"Alipay",
        },
        @{
            @"title" : @"支付宝杉德h5支付",
            @"version" : @"1.0.3",
            @"platform" : @"Alipay",
        },
        @{
            @"title" : @"支付宝fly h5支付",
            @"version" : @"1.0.4",
            @"platform" : @"Alipay",
        },
    ];
    [self.tableView reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.aryData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell    = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    NSInteger        t       = indexPath.row % 2;
    cell.backgroundColor     = t == 0 ? UIColor.grayColor  : UIColor.whiteColor;
    cell.textLabel.textColor = t == 0 ? UIColor.whiteColor : UIColor.blackColor;

    cell.textLabel.text = self.aryData[indexPath.row][@"title"];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];

    //"os":"android", //客户端的操作系统， android或者iOS
    //"productId":"1034", //产品id，可以任意填写
    //"amount":"1", //产品数量 "appId":"app-1",
    //业务的appId，需要与中台配置一致 "appVersion":"1.0.1",
    //版本号，需要与中台配置一致 "packageName":"x.y.z",
    //包名，需要与中台配置一致p "platform":"Alipay"
    //支付平台，Alipay或者Wechat
    NSString     *api    = @"http://117.50.15.225:8081/demo/payment/payOrder";
    NSDictionary *source = self.aryData[indexPath.row];
    NSDictionary *params = @{
        @"os" : @"ios", // ios 直接返回服务器处理好的数据,  android 返回obj数据,前端处理后进行openurl
        @"amount" : @"1",
        @"appId" : @"app-1",
        @"productId" : @"1",
        @"appVersion" : source[@"version"],
        @"packageName" : lanB_BundleId(),
        @"platform" : source[@"platform"],
    };

    [LanBNetworking postWithUrl:api refreshCache:YES params:params success:^(id _Nonnull response) {
      NSLog(@"%@", response);
        NSString *status = response[@"status"];
        if ([@"SUCCESS" isEqualToString:status]) {
            // 支付参数
            NSString *param = response[@"data"][@"param"];
            [LanBDepositer.shared lanb_depositWithInfo:param openUrlBlock:^(BOOL complete, NSURL * _Nonnull url) {
                NSLog(@"------deposit------:%@ \n------openUrl------:%@", complete?@"跳转支付":@"未跳转支付", url);
            }];
        }else{
            __block NSString *error = nil;
            NSArray *errors = response[@"errors"];
            [errors enumerateObjectsUsingBlock:^(NSDictionary*  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                error = obj[@"msg"];
            }];
            [self.view makeToast:error ?: @"数据异常,请联系中台组"
                        duration:3.f
                        position:CSToastPositionCenter];
        }
    } fail:^(NSError *_Nonnull error) {
        NSLog(@"%@", error);
        [self.view makeToast:error.description ?: @"数据异常,请联系中台组"
        duration:3.f
        position:CSToastPositionCenter];
    }];
}

#pragma mark - set get
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView                 = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.backgroundColor = UIColor.whiteColor;
        _tableView.delegate        = self;
        _tableView.dataSource      = self;
        [_tableView registerClass:UITableViewCell.class forCellReuseIdentifier:@"UITableViewCell"];
        [self.view addSubview:_tableView];
        @weakify(self)
        [_tableView mas_updateConstraints:^(MASConstraintMaker *make) {
          @strongify(self)
          CGFloat top = UIApplication.sharedApplication.statusBarFrame.size.height + self.navigationController.navigationBar.frame.size.height;
          make.top.equalTo(self.view).offset(top);
          make.left.equalTo(self.view).offset(20.f);
          make.bottom.equalTo(self.view).offset(-50.f);
          make.right.equalTo(self.view).offset(-20.f);
        }];
    }
    return _tableView;
}

@end
